package com.yxd.bdmapdemo.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * Created by Administrator on 2017/5/16.
 */

public class NetUtils {

    public static boolean checkOnlineState() {
        ConnectivityManager CManager = (ConnectivityManager) UIUtils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        try {
            if (NInfo != null && NInfo.isConnectedOrConnecting()) {
                /*if (InetAddress.getByName(Constant.URLS.BASEURL).isReachable(1000)) {
                    Log.d("demo", "reach");
                    // host reachable
                    return true;
                } else {
                    // host not reachable
                    Log.d("demo", "not reach");
                    return true;
                }*/
                return true;
            } else {
                return false;
                // Log.d("demo","not connect");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


   /* *//**
     * 没有网络
     *//*
    public static final int NETWORKTYPE_INVALID = 0;
    *//**
     * wap网络
     *//*
    public static final int NETWORKTYPE_WAP = 1;
    *//**
     * 2G网络
     *//*
    public static final int NETWORKTYPE_2G = 2;
    *//**
     * 3G和3G以上网络，或统称为快速网络
     *//*
    public static final int NETWORKTYPE_3G = 3;
    *//**
     * wifi网络
     *//*
    public static final int NETWORKTYPE_WIFI = 4;*/

    /**
     * 获取网络状态，wifi,wap,2g,3g.
     *
     * @param context 上下文
     * @return int 网络状态
     */
    public static String getNetWorkType() {
        //int mNetWorkType = 0;
        String type="";
        ConnectivityManager manager = (ConnectivityManager) UIUtils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {

            type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase("WIFI")) {
                type = "WIFI";
                // mNetWorkType = NETWORKTYPE_WIFI;
            } else if (type.equalsIgnoreCase("MOBILE")) {
                String proxyHost = android.net.Proxy.getDefaultHost();
                if (TextUtils.isEmpty(proxyHost)) {
                    if (isFastMobileNetwork()) {
                        type = "3G";
                    } else {
                        type = "2G";
                    }
                } else {
                    type = "wap";
                }

            }
        } else {
            // mNetWorkType = NETWORKTYPE_INVALID;
            type = "";
        }

        return type;
    }

    private static boolean isFastMobileNetwork() {
        TelephonyManager telephonyManager = (TelephonyManager) UIUtils.getContext().getSystemService(Context.TELEPHONY_SERVICE);
        switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_LTE:
                return true; // ~ 10+ Mbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;
            default:
                return false;
        }
    }
}
